{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "import importlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"MODEL_DIR\"] = '../model'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nlpaug.augmenter.char as nac\n",
    "import nlpaug.augmenter.word as naw\n",
    "import nlpaug.augmenter.sentence as nas\n",
    "import nlpaug.flow as nafc\n",
    "\n",
    "from nlpaug.util import Action"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Synonym Augmenter (WordNet, Spanish)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original:\n",
      "Un rápido zorro marrón salta sobre el perro perezoso\n",
      "Augmented Text:\n",
      "Un rápido zorro marrón salta sobre el can perezoso\n"
     ]
    }
   ],
   "source": [
    "text = 'Un rápido zorro marrón salta sobre el perro perezoso'\n",
    "aug = naw.SynonymAug(aug_src='wordnet', lang='spa')\n",
    "augmented_text = aug.augment(text)\n",
    "print(\"Original:\")\n",
    "print(text)\n",
    "print(\"Augmented Text:\")\n",
    "print(augmented_text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Word Embeddings Augmenter (word2vec, French)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original:\n",
      "Un rápido zorro marrón salta sobre el perro perezoso\n",
      "Augmented Text:\n",
      "Un rápido zorro marrón salta sobre el perro moroso\n"
     ]
    }
   ],
   "source": [
    "augmented_text = aug.augment(text)\n",
    "print(\"Original:\")\n",
    "print(text)\n",
    "print(\"Augmented Text:\")\n",
    "print(augmented_text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Word Embeddings Augmenter (fasttext, Japanese)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original:\n",
      "速い茶色の狐が怠惰なな犬を飛び越えます\n",
      "Augmented Text:\n",
      "速い 茶色 後に 狐 が 苦 な な 犬 から 飛び越え ます\n"
     ]
    }
   ],
   "source": [
    "# https://github.com/taishi-i/nagisa\n",
    "import nagisa\n",
    "def tokenizer(x):\n",
    "    return nagisa.tagging(text).words\n",
    "\n",
    "text = '速い茶色の狐が怠惰なな犬を飛び越えます'\n",
    "aug = naw.WordEmbsAug(model_type='fasttext', tokenizer=tokenizer,\n",
    "                      model_path=os.path.join(os.environ.get(\"MODEL_DIR\"), 'wiki.ja.vec'))\n",
    "augmented_text = aug.augment(text)\n",
    "print(\"Original:\")\n",
    "print(text)\n",
    "print(\"Augmented Text:\")\n",
    "print(augmented_text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Contextual Word Embeddings Augmenter (BERT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original:\n",
      "Bonjour, J'aimerais une attestation de l'employeur certifiant que je suis en CDI.\n",
      "Augmented Text:\n",
      "bonjour , j ' aimerais ! attestation de l ' employeur certifiant comment je suis en cdi .\n"
     ]
    }
   ],
   "source": [
    "# Augment French by BERT\n",
    "aug = naw.ContextualWordEmbsAug(model_path='bert-base-multilingual-uncased', aug_p=0.1)\n",
    "text = \"Bonjour, J'aimerais une attestation de l'employeur certifiant que je suis en CDI.\"\n",
    "augmented_text = aug.augment(text)\n",
    "print(\"Original:\")\n",
    "print(text)\n",
    "print(\"Augmented Text:\")\n",
    "print(augmented_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original:\n",
      "速い茶色の狐が怠惰なな犬を飛び越えます\n",
      "Augmented Text:\n",
      "速 い 黄 色 の 狐 か 怠 惰 なな 犬 を 飛 ひ 越 えます\n"
     ]
    }
   ],
   "source": [
    "# Augment Japanese by BERT\n",
    "aug = naw.ContextualWordEmbsAug(model_path='bert-base-multilingual-uncased', aug_p=0.1)\n",
    "text = '速い茶色の狐が怠惰なな犬を飛び越えます'\n",
    "augmented_text = aug.augment(text)\n",
    "print(\"Original:\")\n",
    "print(text)\n",
    "print(\"Augmented Text:\")\n",
    "print(augmented_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original:\n",
      "Un rápido zorro marrón salta sobre el perro perezoso\n",
      "Augmented Text:\n",
      "un rapido gato marron salta sobre el perro perezoso\n"
     ]
    }
   ],
   "source": [
    "# Augment Spanish by BERT\n",
    "aug = naw.ContextualWordEmbsAug(model_path='bert-base-multilingual-uncased', aug_p=0.1)\n",
    "text = 'Un rápido zorro marrón salta sobre el perro perezoso'\n",
    "augmented_text = aug.augment(text)\n",
    "print(\"Original:\")\n",
    "print(text)\n",
    "print(\"Augmented Text:\")\n",
    "print(augmented_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
